﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Web;

namespace SalaryCom.Cool.Gadgets
{
    public class Seog_Jmt_Cookie
    {
        private string CookieDomain => ConfigurationManager.AppSettings["CookieDomain"];
        Seog_Jmt_Log objLog = new Seog_Jmt_Log();

        public string SalGetUserHRIDFromDomainCookie(HttpCookieCollection httpCookie)
        {
            var hrID = httpCookie != null && httpCookie["mhr"] != null && httpCookie["mhr"]["mhi"] != null ? httpCookie["mhr"]["mhi"] : "";
            var myHRID = HttpContext.Current.Server.UrlDecode(hrID);
            return myHRID;
        }


        public IList<Seom_PropertyInfo> SalGetTrackPropertyInfoListFromCookie(HttpContextBase requestContext, string strRequestPageUrl)
        {
            IList<Seom_PropertyInfo> lstPropertyJsonInfo = new List<Seom_PropertyInfo>();
            Dictionary<string, string> dicVal = new Dictionary<string, string>();
            HttpCookie cok = requestContext.Request.Cookies["b2b_utm_digtial"];
            if (cok != null)
            {
                NameValueCollection nameValueCollection = cok.Values;
                foreach (string key in nameValueCollection.AllKeys.Where(i => i != null))
                {
                    if (string.Compare(key, "Digital_Landing_Page_Detail__c", true) == 0)
                    {
                        dicVal.Add(key, System.Web.HttpUtility.UrlDecode(cok[key]));
                    }
                    else
                    {
                        dicVal.Add(key, cok[key]);
                    }
                }
            }

            #region used for tracking cookie is enabled or not.
            if (cok == null)
            {
                objLog.SalError($"SalGetTrackPropertyInfoListFromCookie(url: {strRequestPageUrl}, b2b_utm_digtial cookie key does not exist. May be disabled cookies or delete cookies before submitting)");
            }
            else
            {
                if (string.IsNullOrEmpty(cok.Value))
                {
                    objLog.SalError($"SalGetTrackPropertyInfoListFromCookie(url:{strRequestPageUrl}, b2b_utm_digtial cookie value is empty. The cookie value may be modified before submitting)");
                }
            }
            #endregion 


            if (dicVal.Count != 0)
            {
                foreach (var prop in dicVal)
                {
                    lstPropertyJsonInfo.Add(new Seom_PropertyInfo
                    {
                        PropertyType = prop.Key,
                        PropertyValue = prop.Value
                    });
                }
            }

            lstPropertyJsonInfo.Add(new Seom_PropertyInfo
            {
                PropertyType = "Digital_Device__c",
                PropertyValue = requestContext.Request.Browser.IsMobileDevice ? "m" : "d"
            });

            lstPropertyJsonInfo.Add(new Seom_PropertyInfo
            {
                PropertyType = "Digital_CTA_URL__c",
                PropertyValue = strRequestPageUrl.IndexOf('?') > 0 ? strRequestPageUrl.Split('?')[0].ToString() : strRequestPageUrl
            });
            lstPropertyJsonInfo.Add(new Seom_PropertyInfo
            {
                PropertyType = "Digital_CTA_URL_Detail__c",
                PropertyValue = strRequestPageUrl
            });
            return lstPropertyJsonInfo;
        }


        public void SalSetSalTrackingPropertyCookie(HttpContextBase requestContext, Hashtable hshProperty, bool bolHasNewUtmValue)
        {
            try
            {
                var arrUTMPropertyName = Seog_Jmt_Constant.Conn_UTMPropertyName;
                HttpCookie cookieProperty = new HttpCookie("s_tr_p");
                SalGetSalTrackingPropertyCookie(requestContext);
                //has new utm value, all six utm value use new set
                if (bolHasNewUtmValue)
                {
                    //has one or more new utm value, all six utm value use new set of value
                    for (int intIndex = 0; intIndex < arrUTMPropertyName.Length; intIndex++)
                    {
                        cookieProperty[arrUTMPropertyName[intIndex]] = HttpContext.Current.Server.UrlEncode(hshProperty[arrUTMPropertyName[intIndex]].ToString());
                    }
                }
                else
                {
                    for (int intIndex = 0; intIndex < arrUTMPropertyName.Length; intIndex++)
                    {
                        cookieProperty[arrUTMPropertyName[intIndex]] = SalGetSalTrackingPropertyCookie(requestContext, arrUTMPropertyName[intIndex]);
                    }
                }

                cookieProperty["landing_page"] = HttpContext.Current.Server.UrlEncode(hshProperty["landing_page"].ToString());

                //cookieProperty.Domain = strCookieDomain;
                cookieProperty.Expires = DateTime.Now.AddDays(30);
                requestContext.Response.Cookies.Add(cookieProperty);
            }
            catch (Exception ex)
            {
                objLog.SalError("SalSetSalTrackingPropertyCookie", ex);
                return;
            }

        }

        public string SalGetSalTrackingPropertyCookie(HttpContextBase requestContext)
        {
            HttpCookieCollection httpCookie = requestContext.Request.Cookies;
            string strTrackingProperty = httpCookie != null && httpCookie["s_tr_p"] != null ? httpCookie["s_tr_p"].Value.ToString() : string.Empty;
            return strTrackingProperty;
        }

        public string SalGetSalTrackingPropertyCookie(HttpContextBase requestContext, string subname)
        {
            HttpCookieCollection httpCookie = requestContext.Request.Cookies;
            string strTrackingProperty = httpCookie != null && httpCookie["s_tr_p"] != null && httpCookie["s_tr_p"][subname] != null ? httpCookie["s_tr_p"][subname].ToString() : string.Empty;
            return strTrackingProperty;
        }
    }
    public class Seom_PropertyInfo
    {
        public string PropertyType { get; set; }
        public string PropertyValue { get; set; }
    }
}

